package com.spark.mooc.ch2_scala.part02_objectOrientedProgramming

import java.io.File

import scala.collection.mutable.Map
import scala.io.Source

/**
 * @description:
 * @time: 2020/11/24 23:54
 * @author: lhy
 */
object WordCount {
    def main(args: Array[String]): Unit = {
        val dirFiles = new File("/root/data.txt")
        val files = dirFiles.listFiles()
        val results = Map.empty[String,Int]
        for (file <- files){
            val data = Source.fromFile(file)
            val strs = data.getLines().flatMap(s => s.split(" "))
            strs foreach(word =>
                if (results.contains(word)) results(word) += 1
                else results(word) = 1
            )
        }
        results foreach{case (k,v) => println(s"$k:$v")}
    }
}
